import React from "react"

import http from "../helper/request"
import { IMemberInfo, IMemberInfoParams, IMemberLabelParams, IMemberPageLabelParams, IMemberPageLabelResponse } from "./types"

const useApi = () => {
	const { useState } = React
	const [loading, setLoading] = useState(false)
	async function generalHttp<T>(url: string, body: object | undefined) {
		setLoading(true)
		const ret = await http.post<T>(url, body)
		setLoading(false)
		return ret
	}

	const queryUserLabelPageLabelApi = (body: IMemberPageLabelParams) => {
		const ret = generalHttp<Business.TableResourceType<IMemberPageLabelResponse>>("/userLabel/pageLabel", body)

		return ret
	}

	const updateMemberLabelApi = (body: IMemberLabelParams) => generalHttp("/userLabel/addOrEditMemberLabel", body)
	const deleteMemberLabelApi = (body: Service.NormalIDParams) => generalHttp("/userLabel/deleteLabel", body)
	const getMemberMemberInfoByLabelId = (body: IMemberInfoParams) =>
		generalHttp<Business.TableResourceType<IMemberInfo>>("/userLabel/memberInfoByLabelId", body)

	return {
		loading,
		queryUserLabelPageLabelApi,
		updateMemberLabelApi,
		deleteMemberLabelApi,
		getMemberMemberInfoByLabelId
	}
}

export default useApi
